package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.Apparatus;
import com.biovr.backend.repository.ApparatusRepository;
import com.biovr.backend.service.ApparatusServiceImpl;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by monodev on 2017/5/8.
 */
@Api(value = "/apparatuses",description = "仪器仪器API", consumes="application/json")
@RestController
@RequestMapping("/apparatuses")
public class ApparatusRestController {
    @Autowired
    private ApparatusRepository apparatusRepository;
    @Autowired
    private ApparatusServiceImpl apparatusService;


    /**
     * 查找所有仪器
     * @return
     */
    @PreAuthorize("hasAnyRole('ADMIN','SUPER')")
    @ApiOperation(value="查找所有仪器",response = Apparatus.class,responseContainer = "List")
    @GetMapping
    public RestResponse<List> getAll(){
        List allApparatus =apparatusRepository.findAll();
        return  ResponseFactory.createRestResponse(true,"",allApparatus);
    }

    /**
     * 查找所有status=true仪器
     * @return
     */
    @ApiOperation(value="查找所有仪器",response = Apparatus.class,responseContainer = "List")
    @GetMapping("/actived")
    public RestResponse<List> getAllActived(){
        List allApparatus =apparatusRepository.findByStatus(true);
        return  ResponseFactory.createRestResponse(true,"",allApparatus);
    }

    /**
     * 查找单个仪器
     * @param id
     * @return
     */
    @ApiOperation(value="根据ID查找仪器",response = Apparatus.class)
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public RestResponse<Apparatus> findApparatus(@PathVariable String id){
        Apparatus tarApparatus = apparatusRepository.findOne(id);
        return  ResponseFactory.createRestResponse(true,"",tarApparatus);
    }

    /**
     * 新增仪器仪器
     * @param request
     * @param apparatus
     * @return
     */
    @PostMapping
    @ApiOperation(value="新增/修改仪器仪器", notes="使用json格式POST添加/修改仪器",response = Apparatus.class,responseContainer = "List")
    @ApiImplicitParam(name = "apparatus", value = "仪器对象至少包括仪器名称", required = true, dataType = "Apparatus")
    public RestResponse<?> addReagent(HttpServletRequest request, @RequestBody Apparatus apparatus) throws Exception{
        //检查名称
        if(null==apparatus.getName()||apparatus.getName().equals("")){
            return ResponseFactory.createRestResponse(false,"仪器名称不能为空！",null);
        }
        Apparatus addedAppa = apparatusService.addApparatus(request,apparatus);
        return addedAppa==null?
                ResponseFactory.createRestResponse(false,"无法添加仪器",null)
                : ResponseFactory.createRestResponse(true,"添加仪器成功",addedAppa);
    }

    /**
     * 修改实验仪器
     * @param apparatus
     * @return
     * @throws Exception
     */
    @PutMapping
    public RestResponse<Apparatus> updateApparatus(@RequestBody Apparatus apparatus) throws Exception{
        Apparatus tarExpm;
        //检查名称
        if(null==apparatus.getName()||apparatus.getName().equals("")){
            return ResponseFactory.createRestResponse(false,"仪器名称不能为空！",null);
        }
        //检查标准编号
        if(null!=apparatus.getStdCode() && ""!=apparatus.getStdCode()){
            tarExpm = apparatusRepository.findByStdCode(apparatus.getStdCode());
            if(tarExpm!=null&&!tarExpm.getId().equals(apparatus.getId())){
                return ResponseFactory.createRestResponse(false,"仪器标准编号重复！",null);
            }
        }
        SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        apparatus.setUpdateTime(sdf.format(new Date()));
        Apparatus updatedExpm = apparatusRepository.save(apparatus);
        return ResponseFactory.createRestResponse(true,"修改仪器成功",updatedExpm);
    }
    @ApiOperation(value="重新生成二维码")
    @ApiImplicitParam(name = "id", value = "仪器id", required = true, dataType = "String",paramType = "path")
    @PostMapping("/qrcode/{id}")
    public  RestResponse<String> regenerateQrcode(HttpServletRequest request,@PathVariable String id) throws Exception{
        return ResponseFactory.createSuccessResult( apparatusService.regenerateQrcode(request,id));
    }
    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @ApiOperation(value="DELETE根据ID删除仪器", notes="删除单个仪器")
    @ApiImplicitParam(name = "id", value = "删除仪器Id", required = true, dataType = "String",paramType = "path")
    @DeleteMapping("/{id}")
    public RestResponse<?> deleteapparatus(@PathVariable String id) throws Exception{
       apparatusService.deleteApparatus(id);
        return  ResponseFactory.createSuccessResult();
    }

    /**
     * 根据仪器标准名称查找
     * @param stdCode
     * @return
     */
    @ApiOperation(value="根据仪器标准名称查找")
    @ApiImplicitParam(name = "stdCode", value = "仪器标准名称", required = true, dataType = "String",paramType = "path")
    public RestResponse<Apparatus> getByStdCode(@PathVariable String stdCode){
        return ResponseFactory.createSuccessResult(apparatusRepository.findByStdCode(stdCode));
    }


}
